package com.itheima.mall.handler;

import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

    //处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ErrorResult> businessException(BusinessException exception){
        exception.getStackTrace();
        ErrorResult errorResult = exception.getErrorResult();
        log.info("发生业务异常........................");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    //处理系统异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResult> systemException(Exception exception){
        exception.getStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }


}
